package com.base.kongjian.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import cn.mysteel.util.ObjectUtils;

import com.base.kongjian.dao.FooterPrintDAO;
import com.base.kongjian.entity.FooterPrint;
import com.base.kongjian.service.FooterPrintService;
import com.mysteel.page.Page;

@Service("footerPrintService")
public class FooterPrintServiceImpl implements FooterPrintService
{
	@Autowired
	private FooterPrintDAO footerPrintDao;
	
	@Override
	public void saveFP(FooterPrint footerPrint)
	{
		Assert.notNull (footerPrint);
		
		footerPrint.setLastVisitTime (System.currentTimeMillis ());
		
		footerPrintDao.save (footerPrint);
	}

	@Override
	public void deleteFPById(long id)
	{
		FooterPrint fp = footerPrintDao.getById (id);
		
		if(ObjectUtils.notNull (fp))
		{
			footerPrintDao.delete (id);
		}
	}

	@Override
	public void updateFP(FooterPrint footerPrint)
	{
		Assert.notNull (footerPrint);
		FooterPrint fp = footerPrintDao.getById (footerPrint.getId ());
		
		if(ObjectUtils.notNull (fp))
		{
			fp.setLastVisitTime (footerPrint.getLastVisitTime ());
			fp.setStatus (footerPrint.getStatus ());
			fp.setVisitorNum (footerPrint.getVisitorNum ());
			
			footerPrintDao.update (fp);
		}
	}

	@Override
	public FooterPrint getFPById(long id)
	{
		
		return footerPrintDao.getById (id);
	}

	@Override
	public FooterPrint getFPByMap(Map<String, Object> params)
	{
		
		return footerPrintDao.getByMap (params);
	}

	@Override
	public Page<FooterPrint> queryFP(Map<String, Object> params,int currentPageNum, int maxPageRowCount)
	{
		Page<FooterPrint> page = null;
		if (ObjectUtils.isNull(params))
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = footerPrintDao.count(params);
		if (count > 0)
		{
			page = new Page<FooterPrint>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount);
			page.setURL(params);

			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());

			List<Long> FooterPrintIds = footerPrintDao.query(params);

			if (ObjectUtils.notEmpty(FooterPrintIds))
			{
				List<FooterPrint> FooterPrints = new ArrayList<FooterPrint>(FooterPrintIds.size());

				for (long FooterPrintId : FooterPrintIds)
				{
					FooterPrints.add(getFPById (FooterPrintId));
				}

				page.setDatas(FooterPrints);
			}
		}

		return page;
	}

	@Override
	public boolean canVisitor(long memberId, long visitor_MId)
	{
		Map<String, Object> params = new HashMap<String,Object>();
		params.put ("memberId" ,memberId);
		params.put ("visitor_MId" ,visitor_MId);
		FooterPrint fp = footerPrintDao.getByMap (params);
		
		if(ObjectUtils.isNull (fp))
		{
			
			return true;
		}else
		{
			if(fp.getStatus ()==2)
			{
				return false;
			}
			
		}
		
		return true;
	}

	@Override
	public long getVisitorNum(long memberId, long visitor_MId)
	{
		Map<String, Object> params = new HashMap<String,Object>();
		params.put ("memberId" ,memberId);
		params.put ("visitor_MId" ,visitor_MId);
		FooterPrint fp = footerPrintDao.getByMap (params);
		
		if(ObjectUtils.notNull (fp))
		{
			
			return fp.getVisitorNum ();
		}
		
		return 0 ;
	}

	@Override
	public void forbidVisitor(long memberId, long visitor_MId)
	{
		Map<String, Object> params = new HashMap<String,Object>();
		params.put ("memberId" ,memberId);
		params.put ("visitor_MId" ,visitor_MId);
		FooterPrint fp = footerPrintDao.getByMap (params);
		
		if(ObjectUtils.notNull (fp))
		{
			fp.setStatus (2);
			footerPrintDao.update (fp);
		}else
		{
			fp = new FooterPrint ();
			fp.setStatus (2);
			fp.setVisitor_MId (visitor_MId);
			fp.setMemberId (memberId);
			
			saveFP (fp);
		}
	}

	@Override
	public void allowVisitor(long memberId, long visitor_MId)
	{
		Map<String, Object> params = new HashMap<String,Object>();
		params.put ("memberId" ,memberId);
		params.put ("visitor_MId" ,visitor_MId);
		FooterPrint fp = footerPrintDao.getByMap (params);
		
		if(ObjectUtils.notNull (fp))
		{
			fp.setStatus (1);
			footerPrintDao.update (fp);
		}
	}

	@Override
	public void visit(long memberId, long visitor_MId)
	{
		Map<String, Object> params = new HashMap<String,Object>();
		
		params.put ("memberId" ,memberId);
		params.put ("visitor_MId" ,visitor_MId);
		FooterPrint fp = footerPrintDao.getByMap (params);
		
		if(ObjectUtils.notNull (fp))
		{
			fp.setLastVisitTime (System.currentTimeMillis ());
			fp.setVisitorNum (fp.getVisitorNum ()+1);
			footerPrintDao.update (fp);
		}else
		{
			fp = new FooterPrint ();
			fp.setStatus (1);
			fp.setVisitor_MId (visitor_MId);
			fp.setMemberId (memberId);
			fp.setVisitorNum (1);
			saveFP (fp);
		}
	}

	@Override
	public long getAllVisitorNum(long memberId)
	{
		Map<String,Object> params = new HashMap<String,Object>();
		
		params.put ("memberId" ,memberId);
		long num = 0L;
		List<Long> list = footerPrintDao.query (params);
		for(Long id : list)
		{
			FooterPrint fp = footerPrintDao.getById (id);
			if(ObjectUtils.notNull (fp))
			{
				num = num + fp.getVisitorNum ();
			}
		}
		
		return num;
	}
	
}
